<template>
  <div class="carousel">
    <el-row>
      <el-col :span="16">
        <div class="layui-carousel" id="test10">
          <div carousel-item="">
            <div v-for="(image, index) in carouselData.images" :key="index">
              <img
                :style="{ width: carouselData.width, height: carouselData.height }"
                :src="require('@/assets/images/carousel/' + image)"
              />
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
let carouselData = require("@/assets/data/carouselData.json");
export default {
  name: "Carousel",
  data() {
    return {
      carouselData: carouselData,
    };
  },
  mounted() {
    layui.use(["carousel", "form"], function () {
      var carousel = layui.carousel,
        form = layui.form;

      //常规轮播
      carousel.render({
        elem: "#test1",
        arrow: "always",
      });

      //改变下时间间隔、动画类型、高度
      carousel.render({
        elem: "#test2",
        interval: 1800,
        anim: "fade",
        height: "120px",
      });

      //设定各种参数
      var ins3 = carousel.render({
        elem: "#test3",
      });
      //图片轮播
      carousel.render({
        elem: "#test10",
        width: "976px",
        height: "440px",
        interval: 5000,
      });

      //事件
      carousel.on("change(test4)", function (res) {
        console.log(res);
      });

      var $ = layui.$,
        active = {
          set: function (othis) {
            var THIS = "layui-bg-normal",
              key = othis.data("key"),
              options = {};

            othis
              .css("background-color", "#5FB878")
              .siblings()
              .removeAttr("style");
            options[key] = othis.data("value");
            ins3.reload(options);
          },
        };

      //监听开关
      form.on("switch(autoplay)", function () {
        ins3.reload({
          autoplay: this.checked,
        });
      });

      $(".demoSet").on("keyup", function () {
        var value = this.value,
          options = {};
        if (!/^\d+$/.test(value)) return;

        options[this.name] = value;
        ins3.reload(options);
      });

      //其它示例
      $(".demoTest .layui-btn").on("click", function () {
        var othis = $(this),
          type = othis.data("type");
        active[type] ? active[type].call(this, othis) : "";
      });
    });
  },
};
</script>
<style scoped>
.el-row {
  margin-bottom: 20px;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>